package com.yanqu.road.server.cmd;

import com.yanqu.road.connect.AgentServerLinkControl;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.player.Player;
import com.yanqu.road.player.PlayerControl;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import io.netty.channel.ChannelHandlerContext;

import java.util.HashMap;
import java.util.Map;

@Cmd(code = Protocol.A_SYNC_CLEAR_CHAT, desc = "跨服发起，让客户端清空聊天记录")
public class SyncClearChatCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        Map<Long, Player> allPlayer = new HashMap<>(PlayerControl.getOnlinePlayerMap());
        for (Map.Entry<Long, Player> dataEntry : allPlayer.entrySet()) {
            YanQuMessage msg = YanQuMessageUtils.buildMessageByte(Protocol.U_SYNC_CLEAR_CHAT, packet.getBytes());
            msg.setPlayerId(dataEntry.getKey());
            msg.setLen(packet.getLen());
            AgentServerLinkControl.routeClient(msg);
        }
    }
}
